#!/bin/bash

if [ -n "$2" ]
then
	do_date=$2
else
	#echo 必须给我传一个日期
	#exit
	do_date= $(date -d yesterday +%F) 
fi

echo 当前要操作的日期是$do_date


dws_user_user_login_td="
	insert overwrite table dws_user_user_login_td partition (dt='2022-06-14')
	select
		id,
		max(reg_dt),
		sum(count_login)
	from (
			--注册算一次登录
			select
					distinct
					id,
					date_format(create_time, 'yyyy-MM-dd') reg_dt,
					1 count_login
			from dim_user_zip
			where dt = '9999-12-31'
			and date_format(create_time, 'yyyy-MM-dd') <= '2022-06-14'
			union all
			--登录记录，一条算一次
			select user_id,
					date_id,
					count(user_id)
			from dwd_user_login_inc
			where dt = '2022-06-14'
			group by user_id, date_id
		)t
	group by id;
"

case $1 in
"dws_user_user_login_td")
	hive --database edu -e "$dws_user_user_login_td"
	;;
"all")
	hive --database edu -e "$dws_user_user_login_td"
	;;
esac